class Solution {
public:
    int countGoodNodes(vector<vector<int>>& edges) {
        int n = edges.size() + 1;
        vector<vector<int>> vv(n);
        for (auto& e : edges) {
            vv[e[0]].emplace_back(e[1]);
            vv[e[1]].emplace_back(e[0]);
        }
        vector<int> mm(n, 0);

        int ans = 0;
        auto dfs = [&](auto&& dfs, int n, int father) -> int {
            int node = 0;
            bool t = true;
            int ret = 1;
            for (auto child : vv[n]) {
                if (child == father)
                    continue;
                int m = dfs(dfs, child, n);
                ret += m;
                if (node == 0)
                    node = m;
                if (node != m) {
                    t = false;
                }
            }
            if (t) {
                ans++;
                // cout << n << ' ';
            }
            return ret;
        };
        dfs(dfs, 0, -1);
        return ans;
    }
};